package context.render.tiu.texture;

import context.util.Data;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import math.vec2i;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL30;

public abstract class Texture
{
    
    public static void bind(Texture aTexture)
    {
        if (aTexture == null)
        {
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
        }
        else
        {
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, aTexture.mID);
        }
    }
    
    public static void genNullTexture(
            int aID, 
            vec2i aSize)
    {
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, aID);
        ByteBuffer nullBuf = null;
        GL11.glTexImage2D(
            GL11.GL_TEXTURE_2D, 
            0, 
            GL30.GL_RGBA16F, 
            aSize.x, 
            aSize.y, 
            0, 
            GL11.GL_RGBA, 
            GL11.GL_UNSIGNED_BYTE, 
            nullBuf
        );
        GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
    }
    
    
    public int mID;
    
    public vec2i mSize;
    
    
    public Texture(vec2i aSize)
    {
        // Init the texture ID:
        IntBuffer id = Data.get().reserveDataInt(1);
        GL11.glGenTextures(id);
        this.mID = id.get(0);
        // Init the size:
        this.mSize = aSize.copy();
    }
    
    
    public void onEnd()
    {
        GL11.glDeleteTextures(this.mID);
        this.mSize = null;
    }
    
}
